package org.PISS.viewComponents.productProcessing;

import org.PISS.controller.model.Product;
import org.PISS.controller.model.ProductContains;
import org.PISS.services.DAOService;
import org.PISS.utils.CommonUtil;
import org.primefaces.context.RequestContext;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.util.*;

/**
 * Created by admin on 28.12.13.
 */

@ManagedBean
@ViewScoped
public class AddProductBean {

    private String name;
    private String description;
    private List<Product> avalibleProducts;
    private List<Product> addedProducts;
    private List<Product> filteredProductList;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public List<Product> getFilteredProductList() {
        return filteredProductList;
    }

    public void setFilteredProductList(List<Product> filteredProductList) {
        this.filteredProductList = filteredProductList;
    }

    public void save(){
        Product product = new Product();
        product.setName(name);
        product.setDescription(description);
        product.setTypeId(1);
        product.setQualityRating(10);
        DAOService.getInstance().getProductDAO().addProduct(product);
        List<ProductContains> contains = new ArrayList<ProductContains>();
        for(Product prod:addedProducts){
            contains.add(new ProductContains(prod.getProductID(),product.getProductID()));
        }
        DAOService.getInstance().getProductDAO().addProductContains(contains);
        cleanLists();
        update(CommonUtil.getFullComponentID("dataTable"));
    }

    public List<Product> getAvalibleProducts() {
        if(addedProducts==null){
            avalibleProducts=DAOService.getInstance().getProductDAO().getProducts();
        }
        return avalibleProducts;
    }

    public List<Product> getAddedProducts() {
        return addedProducts;
    }

    public void addProduct(Product product){
        if(product!=null){
            if (addedProducts==null)addedProducts=new ArrayList<Product>();

            addedProducts.add(product);
            avalibleProducts.remove(product);
        }
        update("add_dialog_form");
        dialogShow();
    }


    public void removeProduct(Product product){
        if(product!=null){
            avalibleProducts.add(product);
            addedProducts.remove(product);
        }
        update("add_dialog_form");
        dialogShow();
    }
    public void cleanLists(){
        addedProducts=null;
        avalibleProducts=null;
        setDescription(null);
        setName(null);
        update("add_dialog_form");
    }

    public void update(String id){
        RequestContext.getCurrentInstance().update(id);
    }
    public void dialogShow(){
        RequestContext.getCurrentInstance().execute("add_dialog_form:add_dialog.show()");
    }
}
